package com.gujiangbo.application.utils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import java.io.IOException;
import java.util.List;
import java.util.Map;

public class BeanTools {

	private static Log debugLog = LogFactory.getLog(BeanTools.class);

	/**
	 * Bean 对象转换成 JsonString
	 *
	 * @param obj
	 * @return
	 */
	public static String beanToString(Object obj) {
		return JSONObject.toJSONString(obj);
	}

	/**
	 * JsonString 转换成 Bean对象
	 * 
	 * @param jsonStr
	 * @param object
	 * @return
	 */
	public static Object jsonStringToBean(String jsonStr, Object object) {
		ObjectMapper mapper = new ObjectMapper();
		Object obj = new Object();
		try {
			obj = mapper.readValue(jsonStr, object.getClass());
		} catch (Exception e) {
			debugLog.error("对象转换失败，失败原因:" + e.getMessage());
		}
		return obj;
	}

	/**
	 * 将对象转换成JSON字符串
	 * 
	 * @param bean
	 * @return
	 */
	public static String beanObjectToJsonStr(Object bean) {
		{
			String result = "";
			try {
				if (bean == null) {
					return result;
				}
				result = JSON.toJSONString(bean);
			} catch (Exception e) {
				e.printStackTrace();
			}
			return result;
		}
	}

	public static String beanObjectToJsonStr(Object bean, String... fields) {
		String result = "";
		try {
			if (bean == null) {
				return result;
			}
			JsonIgnoreFilter filter = new JsonIgnoreFilter(bean.getClass(), fields);
			result = JSON.toJSONString(bean, filter, new SerializerFeature[0]);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}

/*	public static String beanArrayToJson(List lists) {
		if (lists == null) {
			return "";
		}
		JSONArray jsonObject = JSONArray.fromObject(lists);
		return jsonObject.toString();
	}
*/
	/*private static String beanToString(Object bean, int level) {
		if (bean == null) {
			return null;
		}
		LogParamTool tool = new LogParamTool(bean.getClass().getName(), 1);
		BeanMap map = BeanMap.create(bean);
		Set set = map.entrySet();
		for (Iterator it = set.iterator(); it.hasNext();) {
			Map.Entry entry = (Map.Entry) it.next();
			String key = (String) entry.getKey();
			Object obj = entry.getValue();
			if (obj == null) {
				tool.addParam(key, "");
			} else if (isBaseType(obj)) {
				tool.addParam(key, obj);
			} else if (isArrayType(obj)) {
				tool.addParam(key + " size", ((Object[]) obj).length);
				Object[] objs = (Object[]) obj;
				for (int i = 0; i < objs.length; i++) {
					tool.addParam(key + i, beanToString(objs[i], level + 1));
				}
			} else {
				tool.addParam(key, beanToString(obj, level + 1));
			}
		}
		return tool.toString();
	}
*/
	public static boolean isBaseType(Object obj) {
		if (((obj instanceof String)) || ((obj instanceof Integer)) || ((obj instanceof Long))
				|| ((obj instanceof Byte)) || ((obj instanceof Double)) || ((obj instanceof Short))
				|| ((obj instanceof Boolean)) || ((obj instanceof Float)) || ((obj instanceof Character))
				|| (obj == null)) {
			return true;
		}
		return false;
	}

	public static boolean isArrayType(Object obj) {
		if ((obj instanceof Object[])) {
			return true;
		}
		return false;
	}
}
